package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.common.bean.query.StaffQueryBean;
import com.koron.common.web.bean.StaffBean;
import com.koron.common.web.mapper.StaffMapper;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class OrgStaffList implements ServerInterface{

	private static Logger logger = LogManager.getLogger(OrgStaffList.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<PageInfo> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			StaffQueryBean param = JsonUtils.objectToPojo(req.getData(), StaffQueryBean.class);
			if(StringUtils.isEmpty(param.getDepartmentCode())) {
				message.setCode(Constant.MESSAGE_INT_FAIL);
				message.setDescription("参数不能为空");
				return message;
			}
			PageHelper.startPage(param.getPage(), param.getPageCount());
			StaffMapper mapper = factory.getMapper(StaffMapper.class);
			List<StaffBean> staffs = mapper.selectList(param);
			message.setData(new PageInfo<StaffBean>(staffs));
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return message;
	}
}
